// DrawPolygon
import * as Cesium from "cesium";

/**
 * 绘制面
 */
class DrawPolygon {
  constructor(arg) {
    this.viewer = arg.viewer;
    this.Cesium = arg.Cesium;
    this.callback = arg.callback;
    this.cesiumHandler = arg.cesiumHandler;
    //创建时，区域范围
    this.range=arg.range;
    this._polygon = null;  //活动面
    this._polygonLast = null;  //最后一个面
    this._positions = []; //活动点
    this._positionsJWDList = []; //活动点
    this._entities_point = [];  //脏数据
    this._entities_polygon = [];  //脏数据
    this._polygonData = null; //用户构造面
  }

  //返回最后活动面
  get polygon() {
    return this._polygonLast;
  }

  //返回面数据用于加载面
  getData() {
    return this._polygonData;
  }

  //加载面
  loadPolygon(data) {
    var $this = this;
    return this.viewer.entities.add({
      polygon: {
        hierarchy: new $this.Cesium.PolygonHierarchy(data),
        clampToGround: true,
        show: true,
        fill: true,
        material: $this.Cesium.Color.RED.withAlpha(0.5),
        width: 3,
        outlineColor: $this.Cesium.Color.BLACK,
        outlineWidth: 1,
        outline: false
      }
    });
  }

  //开始绘制
  startCreate() {
    var $this = this;
     // this.cesiumHandler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
     this.cesiumHandler.setInputAction(function (evt) { //单机开始绘制
      var cartesian = $this.getCatesian3FromPX(evt.position);
      if ($this._positions.length == 0) {
        $this._positions.push(cartesian.clone());
      }
      $this.createPoint(cartesian);
      $this._positions.push(cartesian);
       // _positionsJWDList
       // 获取点的世界坐标
       const start_point = $this.viewer.scene.camera.pickEllipsoid(evt.position, $this.viewer.scene.globe.ellipsoid);
       if (start_point) {
         //将笛卡尔三维坐标转为地图坐标（弧度）
         const cartographic = $this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
         //将地图坐标（弧度）转为十进制的度数
         const lat_String = Cesium.Math.toDegrees(cartographic.latitude).toFixed(4);
         const log_String = Cesium.Math.toDegrees(cartographic.longitude).toFixed(4);
         $this._positionsJWDList.push([parseFloat(log_String), parseFloat(lat_String)]);
       }
    }, $this.Cesium.ScreenSpaceEventType.LEFT_DOWN);
     this.cesiumHandler.setInputAction(function (evt) { //移动时绘制面
      if ($this._positions.length < 1) return;
      var cartesian = $this.getCatesian3FromPX(evt.endPosition);
      if ($this._positions.length == 3) {
        if (!$this.Cesium.defined($this._polygon)) {
          $this._polygon = $this.createPolygon();
        }
      }
      $this._positions.pop();
      $this._positions.push(cartesian);

    }, $this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
     this.cesiumHandler.setInputAction(function (evt) {
      if (!$this._polygon) return;
      var cartesian = $this.getCatesian3FromPX(evt.position);
      $this._positions.pop();
      $this._positions.push(cartesian);
      $this.createPoint(cartesian);
      $this._polygonData = $this._positions.concat();
      $this.viewer.entities.remove($this._positions); //移除
      $this._positions = null;
      $this._positions = [];
      var Polygon = $this.loadPolygon($this._polygonData);
      $this._entities_polygon.push(Polygon);
      $this._polygonLast = Polygon;
      if (typeof $this.callback == "function") {
        $this.callback(Polygon);
      }
    }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);

    //取消绘画事件,注意,不再是销毁
    // this.destroy();
    this.cesiumHandler.setInputAction((evt) => {
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.LEFT_DOWN);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
      console.log(112, this._positionsJWDList);
      if (typeof this.callback == "function") {
        console.log(86, "我被执行了");
        this.callback(this.viewer,this._positionsJWDList,this.range);
      }
    }, this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

  }

  //创建面
  createPolygon() {
    var $this = this;
    var polygon = this.viewer.entities.add({
      polygon: {
        hierarchy: new $this.Cesium.CallbackProperty(function () {
          return new $this.Cesium.PolygonHierarchy($this._positions);
        }, false),
        clampToGround: true,
        show: true,
        fill: true,
        material: $this.Cesium.Color.RED.withAlpha(0.5),
        width: 3,
        outlineColor: $this.Cesium.Color.BLACK,
        outlineWidth: 1,
        outline: false
      }
    });
    $this._entities_polygon.push(polygon);
    return polygon;
  }

  //创建点
  createPoint(cartesian) {
    var $this = this;
    var point = this.viewer.entities.add({
      position: cartesian,
      point: {
        pixelSize: 10,
        color: $this.Cesium.Color.YELLOW,
      }
    });
    $this._entities_point.push(point);
    return point;
  }


  //销毁事件
  destroy() {
    if ( this.cesiumHandler) {
       this.cesiumHandler.destroy();
       this.cesiumHandler = null;
    }
  }

  //清空实体对象
  clear() {
    console.log(168, this._entities_point);
    console.log(169, this._entities_polygon);
    console.log(170, this._positions);
    console.log(171, this._polygon);

    //这里我们先用removeAll()方法，但是，本质上不能使用这个
    //肯定会有bug
    this.viewer.entities.removeAll();

    for (var i = 0; i < this._entities_point.length; i++) {
      this.viewer.entities.remove(this._entities_point[i]);
    }
    for (var i = 0; i < this._entities_polygon.length; i++) {
      this.viewer.entities.remove(this._entities_polygon[i]);
    }
    this._polygon = null;  //活动面
    this._polygonLast = null;  //最后一个面
    this._positions = []; //活动点
    this._entities_point = [];  //脏数据
    this._entities_polygon = [];  //脏数据
    this._polygonData = null; //用户构造面
  }

  getCatesian3FromPX(px) {
    var cartesian;
    var ray = this.viewer.camera.getPickRay(px);
    if (!ray) return null;
    cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
    return cartesian;
  }
}

export default DrawPolygon
